package org.example.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.util.ListUtils;
import org.example.domain.SysRole;
import org.example.service.SysRoleService;

import java.util.List;

public class SysRoleListener implements ReadListener<SysRole> {

    private static final int BACH_COUNT = 3;

    private List<SysRole> cacheList = ListUtils.newArrayListWithExpectedSize(BACH_COUNT);

    private SysRoleService sysRoleService;


    public SysRoleListener(SysRoleService sysRoleService) {
        this.sysRoleService = sysRoleService;
    }

    @Override
    public void invoke(SysRole sysRole, AnalysisContext analysisContext) {
        System.err.println("解析出的数据："+sysRole);
        cacheList.add(sysRole);
        if(cacheList.size()>=BACH_COUNT){
            sysRoleService.insertBath(cacheList);
            //清空
            cacheList = ListUtils.newArrayListWithExpectedSize(BACH_COUNT);
        }

    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        System.err.println("解析完成");
        if(cacheList.size()>0){
            sysRoleService.insertBath(cacheList);
        }
    }
}
